//
//  FeedbackViewController.m
//  ebook
//
//  Created by ChenNan on 16/3/29.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "FeedbackViewController.h"
#import "EbookNetRequest.h"
#import "ToastView.h"
#import "SelectTableCell.h"
#import "MBProgressHUD.h"

#define defaultsText @"您可以输入您遇到的问题或是宝贵的建议。阅读问题请指明小说名称与章节，缺书请指明书名与作者，我们将尽快解决并联系您。您也可以在问题页面通过摇一摇手机反馈问题哦。"//@"欢迎吐槽拍砖提意见~(120字以内哦~)"
#define defaultContactText @"QQ / 邮箱 / 手机号"
#define margin 20

@interface FeedbackViewController ()<UITextViewDelegate, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UITextView *contentTextview;
@property (nonatomic, retain) UITextView *contactTextview;

@property (nonatomic, copy  ) NSString *lastText;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"意见反馈";
    self.view.backgroundColor = MTConfigColor(@"MTFeedBack_Bg_Color");
    
    [self.view addSubview:self.tableView];
    [self createBackItem];
}

#pragma mark - tableview
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MTScreenW, MTScreenH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = MTConfigColor(@"MTFeedBack_tableSeparate_Color");
        _tableView.allowsSelection = NO;
    }
    
    return _tableView;
}

#pragma mark - delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    float height = 44;
    switch (indexPath.row) {
        case 0:
            height = 160;
            break;
        case 3:
            height = 200;
        default:
            break;
    }
    return height;
}

-(UITextView *)textViewWithFrame:(CGRect)frame defaultText:(NSString *)text tag:(NSInteger)tag{
    UITextView *textView = [[UITextView alloc] init];
    textView.tag = tag;
    textView.frame = frame;
    textView.text = text;
    textView.layer.borderWidth = 0.3;
    textView.backgroundColor = MTConfigColor(@"MTFeedBack_textViewBg_Color");
    textView.font = [UIFont systemFontOfSize:14.0];
    textView.delegate = self;
    UIColor *borderColor = MTConfigColor(@"MTFeedBack_textViewBg_Color");
    textView.layer.borderColor = borderColor.CGColor;
    if ([textView.text isEqualToString:defaultsText]||[textView.text isEqualToString:defaultContactText]) {
        textView.textColor = MTConfigColor(@"MTFeedBack_textViewText_Color");
    }
    return textView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    switch (indexPath.row) {
        case 0:
        {
            //反馈内容
            _contentTextview = [self textViewWithFrame:CGRectMake(margin, 10, MTScreenW-margin*2, 150) defaultText:defaultsText tag:1];
            [cell.contentView addSubview:_contentTextview];
        }
            break;
        case 1:{
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(margin, 0, MTScreenW -2*margin, 44)];
            label.text = @"留下您的联系方式，以便我们尽快处理问题";
            label.textColor = [UIColor grayColor];
            label.font = [UIFont systemFontOfSize:14];
            [cell.contentView addSubview:label];
        }
            break;
        case 2:{
            //联系方式
            _contactTextview = [self textViewWithFrame:CGRectMake(margin, 0, MTScreenW-margin*2, 44) defaultText:defaultContactText tag:2];
            CGFloat topCorrect = ([_contactTextview bounds].size.height - [_contactTextview contentSize].height);
            topCorrect = (topCorrect <0.0 ?0.0 : topCorrect);
            _contactTextview.contentOffset = (CGPoint){.x =0, .y = -topCorrect/2};
            [cell.contentView addSubview:_contactTextview];
        }
            break;
        case 3:{
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(margin, 20, MTScreenW - margin*2, 44)];
            btn.backgroundColor = MTConfigColor(@"MTNavigationBar_background_Color");
            btn.layer.borderColor = [UIColor whiteColor].CGColor;
            btn.layer.borderWidth = 1.0f;
            btn.layer.cornerRadius = 3.0f;
            [btn setTintColor:[UIColor whiteColor]];
            [btn setTitle:@"提交反馈" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(subBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:btn];
            
            UIButton *serviceQQ = [[UIButton alloc] initWithFrame:CGRectMake(margin, CGRectGetMaxY(btn.frame), MTScreenW - margin*2, 44)];
            [serviceQQ setTitle:[NSString stringWithFormat:@"客服MM的QQ：%@",kServiceQQ] forState:UIControlStateNormal];
            [serviceQQ setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            serviceQQ.titleLabel.font = [UIFont systemFontOfSize:14];
            serviceQQ.titleLabel.textAlignment = NSTextAlignmentCenter;
            [serviceQQ addTarget:self action:@selector(serviceQQClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:serviceQQ];
        }
            
        default:
            break;
    }
    
    return cell;
}

-(void)serviceQQClick:(UIButton *)btn{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = kServiceQQ;
    [ToastView showMsg:@"QQ号已复制" inView:self.view isCenter:YES];
}

- (void)subBtnClick
{
    NSString *content = _contentTextview.text;
    NSString *contact = _contactTextview.text;
    
    if (content.length == 0 || [content isEqualToString:defaultsText]) {
        [ToastView showMsg:@"请输入您的建议" inView:self.view isCenter:YES];
        return;
    }
    if ([_lastText isEqualToString:content]) {
        [ToastView showMsg:@"请输入新内容，勿重复提交" inView:self.view isCenter:YES];
        return;
    }
    if (contact.length == 0 || [contact isEqualToString:defaultContactText]) {
        [ToastView showMsg:@"请输入联系方式" inView:self.view isCenter:YES];
        return;
    }
    
    _lastText = content;
    
    MBProgressHUD *HUD = [[MBProgressHUD alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    HUD.labelText = @"提交中...";
    [self.view addSubview:HUD];
    [HUD show:YES];
    
    __block FeedbackViewController *blockSelf = self;
    [EbookNetRequest postFeedBackContent:content contact:contact Success:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
        [HUD removeFromSuperview];
        [blockSelf back];
        [ToastView showMsg:successMsg inView:self.view isCenter:YES];
    } failed:^(int stateCode, NSString *failMsg) {
        [HUD removeFromSuperview];
        [ToastView showMsg:failMsg inView:self.view isCenter:YES];
    }];
}

//判断是否数字
- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

#pragma mark - textViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    textView.textColor = MTConfigColor(@"MTFeedBack_textViewBeginEdit_Color");
    if ([textView.text isEqualToString:defaultsText]||[textView.text isEqualToString:defaultContactText]) {
        textView.text = @"";
    }
    
    int offset = textView.tag == 2 ? 150:0;//iPhone键盘高度216，iPad的为352
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.5f];
    self.tableView.frame = CGRectMake(0.0f, -offset, MTScreenW, MTScreenH);
    [UIView commitAnimations];
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    if (textView.text.length < 1) {
        textView.text = textView.tag==1?defaultsText:defaultContactText;
        textView.textColor = MTConfigColor(@"MTFeedBack_textViewText_Color");
        }
    self.tableView.frame =CGRectMake(0, 0, MTScreenW, MTScreenH);
}

- (void)textViewDidChange:(UITextView *)textView {
    
    NSInteger number = [textView.text length];
    if (number > 120) {
        number = 120;
        textView.text = [textView.text substringToIndex:number];
    }
}

#pragma mark - 收起键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [_contentTextview resignFirstResponder];
    [_contactTextview resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

@end
